<template>
  <div class="y-conter">
    <div class="y-biaoge">
      <h3 class="y-bold">车辆信息管理</h3>
      <!-- 搜索输入框部分 -->
      <div>
        <el-form
          :inline="true"
          ref="ruleForm"
          :model="ruleForm"
          class="demo-form-inline">
          <el-form-item label="单位名称:">
            <el-select v-model="ruleForm.company_id" placeholder="全部">
              <el-option
                v-for="item in linput1"
                :key="item.company_name"
                :label="item.company_name"
                :value="item.company_id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 按钮搜和索部分 -->
          <el-form-item label="车辆备注:">
            <el-input
              v-model="ruleForm.carinfo_name"
              placeholder="请输入"
              class="padding"
            ></el-input>
            <!-- 按钮搜和索部分 -->
          </el-form-item>
          <el-form-item label="车牌号:">
            <el-input
              v-model="ruleForm.carinfo_number"
              placeholder="请输入"
              class="padding"
            ></el-input>
            <!-- 按钮搜和索部分 -->
          </el-form-item>
          <!-- 按钮搜和索部分 -->
          <el-form-item>
            <el-button type="primary" @click="search('ruleForm')">搜索</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格部分 -->
      <div class="y-table">
        <el-table
          :border="true"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          ref="multipleTable"
          :data="currList"
          tooltip-effect="dark"
          style="width: 98%, text-align: center "
          @selection-change="handleSelectionChange"
        >
          <!-- 序号 -->
          <el-table-column
            align="center"
            type="index"
            width="100"
            :index="indexFn"
            label="序号"
          ></el-table-column>
          <el-table-column
            label="所属单位"
            prop="buildCompany.company_name"
            width="230"
            hight="30px"
            align="center"
          ></el-table-column>
        
          <el-table-column
            label="车辆类型"
            prop="carinfo_type"
            align="center"
          ></el-table-column>
          <el-table-column
            label="车牌号"
            width="100"
            prop="carinfo_number"
            align="center"
          ></el-table-column>
          <el-table-column
            label="车辆状态"
            width="100"
            prop="carinfo_status"
            align="center"
          >
            <template slot-scope="scope">
              {{ scope.row.carinfo_status === 0 ? "进入工地" : "" }}
              {{ scope.row.carinfo_status === 1 ? "离开工地" : "" }}
            </template>
          </el-table-column>
          <el-table-column label="车辆图片" prop="carinfo_url" align="center" width="150px">
					<template slot-scope="scope">
						<el-popover placement="top-start" trigger="click">
							<!--trigger属性值：hover、click、focus 和 manual-->
							<a :href="scope.row.carinfo_url" target="_blank" title="查看最大化图片"><img :src="scope.row.carinfo_url" style="width: 300px;height: 300px" /></a>
							<img slot="reference" :src="scope.row.carinfo_url" style="width: 50px;height: 50px; cursor:pointer" />
						</el-popover>
					</template>
				</el-table-column>
          <el-table-column
            label="车辆备注"
            prop="carinfo_name"
            width="120"
            align="center"
          >
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="bianji(scope.$index, scope.row)">
                编辑
              </el-button>
              <el-button
                type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--修改框  -->

        <el-dialog title="编辑信息" :visible.sync="dialogVisible" width="680px">
          <el-form :model="formInline" status-icon :rules="rules" ref="formInline">
            <el-row>
              <el-col :span="6">
                <el-form-item label="所属单位:">
                  <el-input
                    v-model="formInline.company_name"
                    :disabled="true"
                    class="padding"
                  >
                    </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                  <el-form-item prop="carinfo_status">
                  <div>车辆状态:</div>
                  <el-select
                    v-model="formInline.carinfo_status"
                     :disabled="true"
                    placeholder="请选择">
                    <el-option
                      v-for="item in linput2"
                      :key="item.index"
                      :label="item.label"
                      :value="item.value">
                      </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="车辆类型:">
                  <el-input
                    v-model="formInline.carinfo_type"
                   :disabled="true"
                   class="padding"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="6">
                <el-form-item label="车牌号:">
                  <el-input
                    v-model="formInline.carinfo_number"
                    :disabled="true"
                    class="padding"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="车辆备注:">
                  <el-input
                  class="padding"
                    v-model="formInline.carinfo_name"
                    placeholder="">
                    </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="quxiao('formInline')">取 消</el-button>
            <el-button type="primary" @click="SureEdit('formInline')">确 定</el-button>
          </span>
        </el-dialog>
      </div>
      <!-- 分页器 -->
      <div class="y-fenye">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="tableData.length"
          :page-size="pageSize"
          @current-change="changePage"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
// import axios from "axios";
import http from "../api";
export default {
  data() {
    return {
      arr:{},
      //车辆状态
      linput2: [
        {
          value: 0,
          label: "进入工地",
        },
        {
          value: 1,
          label: "离开工地",
        },
      ],
      linput1: "", //单位输入框
      dialogVisible: false,
      //搜索输入框
      ruleForm: {
       
         company_id: "", //所属单位
       company_name: "", //所属单位
       
      },
      // 弹框
      formInline: {
        carinfo_id:"",//车辆ID
        carinfo_status: "", //车id辆状态
        carinfo_number: "", //车牌号
        carinfo_type: "", //车辆类型
        carinfo_name:"",//车辆备注
        company_name: "", //所属单位
     
      },

      rules: {},
      //多选表格的复选框
      multipleSelection: [],
      // 分页
      tableData: [], //总数居
      currPage: 1, //当前页
      pageSize: 5, //每页数据量
      currList: [], //当前展示的数据数组
      pickerOptions1: {
        //  日期
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      value1: "",
      value2: "",
    };
  },
  computed: {},

  methods: {
    // 编辑
    bianji(index, row) {
      console.log(row);
      this.dialogVisible = true;
       this.formInline.carinfo_id = row.carinfo_id;
      this.formInline.carinfo_name = row.carinfo_name;
      this.formInline.company_name = row.company_name;
      this.formInline.carinfo_type = row.carinfo_type;
      this.formInline.carinfo_status = row.carinfo_status;
      this.formInline.carinfo_number = row.carinfo_number;
      console.log(this.formInline);
      // console.log(this.formInline)
    },

    // 分页
    setCurrList(index) {
      this.currList = this.tableData.slice(
        (index - 1) * this.pageSize,
        index * this.pageSize
      );
      // console.log('当前页',index);
    },
    //改变当前页
    changePage(val) {
      this.currPage = val;
      this.setCurrList(val);

      // console.log(val);
    },
    // 表格序号
    indexFn(index) {
      index = index + 1 + (this.currPage - 1) * this.pageSize;
      return index;
    },
    //重置按钮
    resetForm(formName) {
      this[formName] = {};
      this.$refs[formName].resetFields();
    },

    // 编辑里的确定按钮
    SureEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogVisible = false;
          http.carcompile(this.formInline).then((r) => {
            console.log("这是编辑的", r);
            http.lineage(this.arr).then((res) => {
              this.tableData = res.data;
              this.setCurrList(1);
              // // 点确定后让表清空
              // this.formInline = [];
            });
          });
        } else {
          this.$message({
            showClose: true,
            message: "修改失败",
            type: "error",
          });
          return false;
        }
      });
    },
      // 编辑里的取消按钮
    quxiao(formInline) {
      // console.log(formInline);
      this.dialogVisible = false;
      // 点取消后清空
      // for (let key in formInline) {
      //   formInline[key] = "";
      // }
    },
    //选中复选框获取下标
    handleSelectionChange(val) {
      // this.multipleSelection = val;
      // let ids = [];
      // this.multipleSelection.map((item) => {
      //   ids.push(item.key);
      // });
      // this.selectedIDs = ids;
      // console.log("你点击的是", this.selectedIDs);
    },

    //搜索输入
    search() {
      console.log('aaaa',this.ruleForm)
        http.lineage(this.ruleForm).then((r) => {
      console.log(r);
      this.tableData = r.data;
      console.log('bbbbb',r);
      this.setCurrList(1);
    });
      // this.$refs[ruleForm].validate((valid) => {
      //   if (valid) {
      //     http.clinput2(this.ruleForm).then((r) => {
      //       console.log("返回的数据", r);
      //       this.tableData = r.data;
      //       this.setCurrList(1);
      //       console.log(this.ruleForm);
      //     });
      //   } else {
      //     console.log("error submit!!");
      //     return false;
      //   }
      // });

    },

     // 表格删除按钮
    handleDelete(index, row) {
      // console.log(row.carinfo_id);
      http.cardel(row).then((r) => {
        http
          .lineage({
           
          })
          .then((r) => {
            console.log(r);
            this.tableData = r.data;
            console.log(r.data);
            // console.log(this.tableData);
            this.setCurrList(1);
          });

        console.log(r);
        console.log("单个删除", r.data);
      });
    },
  },
  //获取数据
  mounted() {
    // 单位输入框
     http.clinput2({}).then((res) => {
      this.linput1 = res.data;
      console.log("单位", res.data);
    });
    // 表格
    console.log("11111")
    http.lineage({}).then((r) => {
      console.log("这是表格",r);
      this.tableData = r.data;
      console.log(r.data);
      this.setCurrList(1);
    });
  },
};
</script>
<style scoped>
.padding >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px !important;
}
.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px;
  /* margin-left: 5px; */
}
/* 大容器 */
.y-conter {
  height: 640px;
  background-color: white;
  overflow: hidden;
  overflow-y: scroll;
  margin-top: 15px;
}
/* 表格 */
.y-biaoge {
  margin-top: 20px;
  margin-left: 10px;
}

.y-table {
  margin-top: 15px;
}
/* 右侧新增导出按钮 */
.y-btn {
  position: absolute;
  top: 164px;
  left: 94%;
}
/* 所有按钮 */
.el-button {
  height: 30px;
  line-height: 0px;
}
/* 标题字体 */
.y-bold {
  font-weight: bold;
}
/* 输入框部分样式 */
.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
}
.el-input >>> .el-input__inner {
  width: 178px;
  height: 30px;
}
.el-range-editor {
  width: 160px;
  height: 30px;
  padding-left: 10px;
}
.el-input >>> .el-range__icon {
  line-height: 0px;
  padding-left: 10px;
}
.el-range-editor >>> .el-input__inner {
  width: 200px;
  height: 30px;
  margin-top: 10px;
}
/* 分页 */
.y-fenye {
  float: right;
  margin-top: 20px;
}
/* 新增 */

.el-form-item {
  margin-bottom: 2px !important;
}
.el-col-6 {
    width: 27%;
    margin-left: 20px;
}
</style>